// mircoApp/gradevin/deviceClass/deviceClass.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    select: 0,
    list: [],
    pageTitle: "商品列表",
    action: [],
    deviceName: "",

    page: 1,
    total_page: 0,
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.device_id) {
      this.setData({
        device_id: options.device_id
      })
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "device_id") {
            this.setData({
              device_id: chil_arr[1],
            });
          }
        }
      }
    }
    this.init();
    this.getData();
  },
  init() {
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.consumer.category.index");
    app._postNetWork({
      url: urlStr,
      data: {
        device_id: this.data.device_id,
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            action: res.data.action,
            deviceName: res.data.current_device.name
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: function (res) {}
    });
  },
  getData() {
    let json = {
      device_id: this.data.device_id,
      page: this.data.page
    };
    if(this.options.member_coupon_id){
      json.member_coupon_id = this.options.member_coupon_id
    }
    let urlStr = app.getNetAddresss("plugin.gradevin.frontend.consumer.category.goods");
    this.resGetData(urlStr, json);
  },
  resGetData(urlStr, json) {
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          if (this.data.active == "2") {
            this.data.total_page = res.data.record.last_page;
            if (!this.data.total_page) {
              this.total_page = 0;
            }
            this.setData({
              list: res.data.record.data
            })
          } else {
            this.setData({
              list: res.data
            })
          }
        } else {
          app.tips(res.msg)
        }
      },
    });
  },
  useCoupon(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (item.status) {
      return;
    }
    wx.showModal({
      content: '是否确认使用该时长券',
      complete: (res) => {
        if (res.confirm) {
          this.useCouponPost(item);
        }
      }
    })
  },
  useCouponPost(item) {
    app._postNetWork({
      url: app.getNetAddresss("plugin.gradevin.frontend.consumer.coupon.use"),
      data: {
        id: item.id,
        device_id: this.data.device_id
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.setData({
            page: 1,
            list: [],
            total_page: 0,
          })
          this.tapNavLeft(this.data.active)
        } else {
          app.tips(res.msg)
        }
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore && this.data.active == "2") {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        device_id: this.data.device_id,
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page
      }
      if(this.options.member_coupon_id){
        json.member_coupon_id = this.options.member_coupon_id
      }
      urlStr = app.getNetAddresss('plugin.gradevin.frontend.consumer.coupon.index');
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              list: this.data.list.concat(res.data.record.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  to_Order() {
    // goodsId=216256&optionsId=1678908&total=1&tag=-2&mark=&fromStock=&device_id=1
    console.log(this.data.select, this.data.list.has_many_options[this.data.select], 'to_Order')
    let submitActionTag = "-2"; // 立即购买
    let _total = 1;
    let {
      goods_id,
      id
    } = this.data.list.has_many_options[this.data.select];
    wx.navigateTo({
      url: `/packageD/buy/myOrder_v2/myOrder_v2?goodsId=${goods_id}&optionsId=${id}&total=${_total}&tag=${submitActionTag}&device_id=${this.data.device_id}&is_gradevin_sing=1`
    })
  },
  toGoods(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/packageA/detail_v2/detail_v2?id=${item.goods_id}&device_id=${this.data.device_id}&cargo_road_id=${item.id}`,
    })
  },
  tapNavLeft(e) {
    let {
      key
    } = e.currentTarget.dataset
    this.data.active = key;
    let urlstr = "";
    if (this.data.active == '2') {
      wx.setNavigationBarTitle({
        title: '时长券列表',
      })
      urlstr = app.getNetAddresss("plugin.gradevin.frontend.consumer.coupon.index");
    } else if (this.data.active == '1') {
      wx.setNavigationBarTitle({
        title: '商品列表',
      })
      urlstr = app.getNetAddresss("plugin.gradevin.frontend.consumer.duration.package");
    } else {
      wx.setNavigationBarTitle({
        title: '商品列表',
      })
      urlstr = app.getNetAddresss("plugin.gradevin.frontend.consumer.category.goods");
    }
    this.setData({
      active: this.data.active,
      select: 0,
      page: 1,
      list: []
    })
    let json = {
      device_id: this.data.device_id,
      page: this.data.page
    };
    if(this.options.member_coupon_id){
      json.member_coupon_id = this.options.member_coupon_id
    }
    this.resGetData(urlstr, json);
  },
  // 选择时长券商品
  tapCouponShop(e) {
    let {
      key
    } = e.currentTarget.dataset
    this.setData({
      select: key
    })
  }
})